app.service("returnOrderService", function($http, ajaxRequest) {
	return {
		getDetailData : function(obj) {
			return ajaxRequest.request($http.get("/purchaseReturnOrder/finaceCheckDetail",
					{
						params : obj
					}));
		},
		getListData:function(obj){
			return ajaxRequest.request($http.get("/purchaseReturnOrder/finaceCheckList",
					{
						params : obj
					}));
		},
		auditReturnOrder:function(obj){
			return ajaxRequest.request($http.get("/purchaseReturnOrder/finaceAudit",{
				params:obj
			}));
		},
		rejectReturnOrder:function(obj){
			return ajaxRequest.request($http.get("/purchaseReturnOrder/finaceReject",{
				params:obj
			}));
		}
	}
}).controller("finacePurchaseReturnDetailController", function($scope, returnOrderService) {
	var orderId = GetQueryString("orderId");
	$scope.searchData = {
			orderId : orderId
		};
	
	var requestDetail = function (){
		returnOrderService.getDetailData($scope.searchData).then(
			function(data) {
				$scope.returnOrder = data.returnOrder;
				$scope.supplierName = data.supplierName;
				$scope.warehouse = data.warehouse;
				$scope.planReturnTime = timeStamp2String(data.planReturnTime);
				$scope.actualReturnTime = timeStamp2String(data.actualReturnTime);
				$scope.address = data.address;
				$scope.remark = data.remark;
				$scope.amount = data.amount;
				$scope.returnOrderList = data.lstGoods;
			}, function(data) {
				Kalert(data.msg);
			});
	}
	
	requestDetail();
	
	$scope.back = function() {
		window.location.href = "/purchaseReturnOrder/index";
	}	
}).filter('cut', function () {
	  return function (value, wordwise, max, tail) {
		    if (!value) return '';

		    max = parseInt(max, 10);
		    if (!max) return value;
		    if (value.length <= max) return value;

		    value = value.substr(0, max);
		    if (wordwise) {
		      var lastspace = value.lastIndexOf(' ');
		      if (lastspace != -1) {
		        value = value.substr(0, lastspace);
		      }
		    }

		    return value + (tail || ' …');
	};
}).controller("finacePurchaseReturnListController",function($scope,returnOrderService){
	$scope.orderState=0;
	$scope.purchaseType=1;
	$scope.searchData = {
			orderState : $scope.orderState,
			returnOrder : $scope.returnOrder,
			supplierCode : $scope.supplierCode,
			supplierName : $scope.supplierName,
			purchaseType : $scope.purchaseType,
			returnPerson : $scope.purchasePerson,
			startReturnTime : $scope.startTime,
			endReturnTime : $scope.endTime,
			pageIndex : 1,
			pageSize : 10
	};
		
	var requestListData = function() {
		$scope.searchData.orderState=$scope.orderState;
		$scope.searchData.returnOrder = $scope.returnOrder;
		$scope.searchData.supplierCode = $scope.supplierCode;
		$scope.searchData.supplierName = $scope.supplierName;
		$scope.searchData.purchaseType = $scope.purchaseType;
		$scope.searchData.returnPerson = $scope.purchasePerson;
		$scope.searchData.startReturnTime = $scope.startTime;
		$scope.searchData.endReturnTime = $scope.endTime;
		
		returnOrderService.getListData($scope.searchData).then(
				function(data) {
					var re = data.result;
					$scope.returnOrderList = re.entityList;
					$scope.CurrentPage = re.curPageIndex;
					$scope.RecordCount = re.recordCount;
					$scope.ShowPages = re.pageCountList;
					$scope.PageCount = re.pageCount;
				}, function(data) {
					Kalert(data.msg);
				});
	}
	
	requestListData();
	
	$scope.GotoPage = function(pageindex) {
		if (pageindex <= 0)
			return;
		if (pageindex > $scope.PageCount)
			return;
		$scope.searchData.pageIndex = pageindex;
		requestListData($scope.searchData);
	};
	
	$scope.Search = function() {
		$scope.searchData.pageIndex = 1;
		requestListData();
	}
	
	$scope.ShowDetail = function(orderId) {
        window.location.href="/purchaseReturnOrder/edit?orderId="+orderId;
	}
		
	$scope.Audit=function(orderId,orderCode,orderState){
		$scope.checkData = {
				orderReturnId : orderId,
				orderReturnCode:orderCode
			};
		returnOrderService.auditReturnOrder($scope.checkData).then(
				function(data) {
					Kalert(data.msg,"系统提示",requestListData());
				}, function(data) {
					Kalert(data.msg);
		});
	}
	
	$scope.Reject = function(orderId,orderCode,orderState){
		$scope.checkData = {
				orderReturnId : orderId,
				orderReturnCode:orderCode
			};
		returnOrderService.rejectReturnOrder($scope.checkData).then(
				function(data) {
					Kalert(data.msg,"系统提示",requestListData());
				}, function(data) {
					Kalert(data.msg);
		});
	}
	
	//点击标签
	$scope.tabClick = function(id,state){
		$scope.orderState = state;
		RemoveCss();
		$("#" + id).addClass("active");
		$scope.searchData.pageIndex = 1;
		requestListData();
	}
	var RemoveCss = function() {
		$("#286433").removeClass("active");
		$("#286434").removeClass("active");
		$("#286435").removeClass("active");
	};
	
	$scope.Reset = function() {
		//$scope.orderState=0;
		$scope.returnOrder=undefined;
		$scope.supplierCode=undefined;
		$scope.supplierName=undefined;
		$scope.purchaseType=1;
		$scope.purchasePerson=undefined;
		$scope.startTime=undefined;
		$scope.endTime=undefined;		
	}
	
});

function GetQueryString(sProp) {
	var re = new RegExp("[&,?]" + sProp + "=([^//&]*)", "i");
	var a = re.exec(window.location.search);
	if (a == null) {
		return "";
	} else {
		return a[1];
	}
}


function timeStamp2String (time){
    var datetime = new Date();
     datetime.setTime(time);
     var year = datetime.getFullYear();
     var month = datetime.getMonth() + 1;
     var date = datetime.getDate();
     var hour = datetime.getHours();
     var minute = datetime.getMinutes();
     var second = datetime.getSeconds();
     return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
};